﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ECS.DAL.Entity;
using ECS.DAL.Component;
using ECS.Framework;

namespace E_ICS.Master
{
    public partial class frmLocation : Form
    {
        #region private properties
        private LocationComponent _dataComponent = new LocationComponent();
        //private List<ObjectContext> _zoneList = new List<ObjectContext>();
        SortableBindingList<Location> _gridSource = new SortableBindingList<Location>();
        List<ObjectContext> _deleteList = new List<ObjectContext>();
        private bool _isLoadedData = false;
        #endregion

        #region form event
        public frmLocation()
        {
            InitializeComponent();
        }

        private void frmLocation_Load(object sender, EventArgs e)
        {
            List<ObjectContext> _zoneList = new List<ObjectContext>();
            try
            {
                dgvZone.AutoGenerateColumns = false;
                _zoneList = _dataComponent.Search(null);
                foreach (ObjectContext item in _zoneList)
                {
                    _gridSource.Add((Location)item);
                }
                SetGridData();
                _isLoadedData = true;
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Close();
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmLocation_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result;
            try
            {
                result = CheckSaves();
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }

        private void dgvZone_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (_gridSource.Count > 0)
                {
                    if (e.ColumnIndex == dgvZone.Columns["btnDelete"].Index && e.RowIndex >= 0)    // Delete Dim
                    {
                        if (CommonMessageBox.ShowMessageConfirmDelete() == DialogResult.Yes)
                        {
                            if (_gridSource[e.RowIndex].GetObjectSate() != OBJECT_STATE.Insert)
                            {
                                Location obj = _gridSource[e.RowIndex];
                                obj.AppId = GlobalVariable.user.ApplicationID;
                                obj.UserId = GlobalVariable.user.UserID;
                                obj.Device = GlobalVariable.user.Device;
                                obj.DeleteObject();
                                _dataComponent.Delete((ObjectContext)_gridSource[e.RowIndex]);
                            }
                            _gridSource.RemoveAt(e.RowIndex);
                            setRowNumber((DataGridView)sender);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }

        private void dgvZone_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            try
            {
                if (_isLoadedData)
                {
                    //DataGridView dgv = (DataGridView)sender;
                    //if (dgv.Rows.Count > 0)
                    //{
                    //    dgv.Rows[e.RowIndex - 1].HeaderCell.Value = String.Format("{0}", e.RowIndex);
                    //}
                    setRowNumber(dgvZone);
                }
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }
        #endregion

        #region private methods
        /// <summary>
        /// set grid data source
        /// </summary>
        private void SetGridData()
        {
            dgvZone.DataSource = _gridSource;
            setRowNumber(dgvZone);
        }
        /// <summary>
        /// Check data in grid which are changed then save change
        /// </summary>
        /// <returns></returns>
        private DialogResult CheckSaves()
        {
            bool isChange = false;
            DialogResult result = System.Windows.Forms.DialogResult.No;
            //_gridSource = (BindingList<Location>)dgvZone.DataSource;
            if (_gridSource == null)
            {
                return result;
            }
            foreach (ObjectContext item in _gridSource)
            {
                if (item.ObjectStatus != OBJECT_STATE.None)
                {
                    isChange = true;
                    break;
                }
            }
            if (_deleteList.Count > 0)
            {
                isChange = true;
            }
            //foreach (Location item in _temp)
            {
                if (isChange)
                {
                    result = CommonMessageBox.displayConfirmSave();
                    if (result == System.Windows.Forms.DialogResult.Yes)
                    {
                        foreach (Location data in _gridSource)
                        {
                            //if (data.ObjectStatus == OBJECT_STATE.Insert)
                            {
                                data.AppId = GlobalVariable.user.ApplicationID;
                                data.Device = GlobalVariable.user.Device;
                                data.UserId = GlobalVariable.user.UserID;
                            }
                        }
                        _dataComponent.SaveChanges(_gridSource.ToList<ObjectContext>());
                        _dataComponent.SaveChanges(_deleteList);
                        _deleteList.Clear();
                        //break;
                    }
                }
            }
            return result;
        }
        /// <summary>
        /// set row number
        /// </summary>
        /// <param name="dgv"></param>
        private void setRowNumber(DataGridView dgv)
        {
            foreach (DataGridViewRow row in dgv.Rows)
            {
                if (row.Index == dgv.Rows.Count - 1)
                {
                    row.HeaderCell.Value = "";
                }
                else
                {
                    row.HeaderCell.Value = String.Format("{0}", row.Index + 1);
                }
            }
        }
        #endregion

        

        
    }
}
